package com.qbm.association.config;

//import com.qbm.association.interceptor.TokenInterceptor;
import com.qbm.association.interceptor.TokenInterceptor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.web.servlet.config.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;


/**
 * 跨域请求支持/token拦截
 * tip:只能写在一个配置类里
 */
@Configuration
public class WebConfiguration implements WebMvcConfigurer{



    private final TokenInterceptor tokenInterceptor;

    //构造方法
    public WebConfiguration(TokenInterceptor tokenInterceptor){
        this.tokenInterceptor = tokenInterceptor;
    }


    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(false)
                .allowedHeaders("*")
                .allowedMethods("*")
                .allowedOrigins("*");
    }

    @Override
    public void configureAsyncSupport(AsyncSupportConfigurer configurer){
        configurer.setTaskExecutor(new ConcurrentTaskExecutor(Executors.newFixedThreadPool(3)));
        configurer.setDefaultTimeout(30000);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/api/file/**").addResourceLocations("file:" + "d:/workspace/img/");
    }


    @Override
    public void addInterceptors(InterceptorRegistry registry){
//        List<String> excludePath = new ArrayList<>();
//        //排除拦截，除了注册登录(此时还没token)，其他都拦截
//        excludePath.add("/wx/*");
//        excludePath.add("/wx/register");
//        excludePath.add("/api/covers");
//        excludePath.add("/api/file/*");
//        excludePath.add("/wx2/123");
//        excludePath.add("/wxlogin");
//        excludePath.add("/exportStudents");
//        excludePath.add("/association/login/studentLogin");
//        excludePath.add("/user/register");  //登录
//        excludePath.add("/association/login");
//        excludePath.add("/association/loginT");//注册
//        excludePath.add("/user/submitForm");
//        excludePath.add("/activity/wxallid");
//        excludePath.add("/activity/wxall");
//        excludePath.add("/static/**");  //静态资源
//        excludePath.add("/assets/**");  //静态资源
//        excludePath.add("/swagger-resources/**");
//        excludePath.add("/doc.html");
//        excludePath.add("/swagger-ui.html");
//        excludePath.add("/association/club/*");
//        registry.addInterceptor(tokenInterceptor)
//                .addPathPatterns("/**")
//                .excludePathPatterns(excludePath)
//                .excludePathPatterns("/swagger-resources/**", "/webjars/**", "/v2/**", "/swagger-ui.html/**");
//
//        WebMvcConfigurer.super.addInterceptors(registry);
    }
}